﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PACKETPARSERMAX20.CORE
{
    public class Packet
    {
        public int Id { get; private set; }
        public DateTime CreatedDate { get; private set; }
        public byte Direction { get; private set; }
        public uint Opcode { get; private set; }
        public uint Ticks { get; private set; }

        public string OpcodeName { get; private set; }
        public byte[] Data { get; private set; }
        public int ClientBuild { get; private set; }
        //public string AccountName { get; private set; }
        public int Size { get; private set; }
        public string Warning { get; set; }
        public System.Text.StringBuilder Result { get; set; }

        public Packet(int pId, DateTime pDateTime, byte pDirection, uint opcode, byte[] pData, int pSize, int clientbuild)//, string pAccountName)
        {
            Id = pId;
            CreatedDate = pDateTime;
            Direction = pDirection;
            Opcode = opcode;
            Data = pData;
            Size = pSize;
            ClientBuild = clientbuild;
            //AccountName = pAccountName;
            OpcodeName = GetOpcodeName();
            if (OpcodeName == null)
                OpcodeName = string.Format("U{0}_UNWKNOWN_{1}", Direction == 1 ? "SMSG" : "CSMG", Opcode);
            Result = new StringBuilder();
        }

        public Packet(int pId, DateTime pDateTime, byte pDirection, uint opcode, byte[] pData, int pSize, int clientbuild, uint pTicks)//, string pAccountName)
        {
            Id = pId;
            CreatedDate = pDateTime;
            Direction = pDirection;
            Opcode = opcode;
            Data = pData;
            Size = pSize;
            ClientBuild = clientbuild;
            Ticks = pTicks;
            OpcodeName = GetOpcodeName();
            if (OpcodeName == null)
                OpcodeName = string.Format("U{0}_UNWKNOWN_{1}", Direction == 1 ? "SMSG" : "CSMG", Opcode);

            Result = new StringBuilder();
        }
        public string GetOpcodeName()
        {
            return GetOpcodeName(Opcode, ClientBuild);
        }
        public static string GetOpcodeName(uint opcode, int clientbuild)
        {


            return Enum.GetName(Type.GetType("PACKETPARSERMAX20.CORE.Opcodes"), opcode);
            //string r = "";

            //if (opcode > 8606)
            //{
            //    Type t = Type.GetType(string.Format("PACKETPARSERMAX20.CORE.Opcodes.Opcode_{0}", clientbuild.ToString().Replace(".", "_")));
            //    if (t != null)
            //    {
            //        r = Enum.GetName(t, opcode);
            //    }

            //    if (r.IsEmpty())
            //    {
            //        t = Type.GetType("PACKETPARSERMAX20.CORE.Opcodes.Opcode_Default");

            //        if (t != null)
            //        {
            //            r = Enum.GetName(t, opcode);
            //        }
            //    }
            //}
            //else
            //{
            //    if (r.IsEmpty())
            //    {
            //        Type t = Type.GetType("PACKETPARSERMAX20.CORE.Opcodes.Opcode_Old");

            //        if (t != null)
            //        {
            //            r = Enum.GetName(t, opcode);
            //        }
            //    }
            //}

            //return r;
        }

        public string HexDump()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("**********************************************************");
            sb.AppendLine(string.Format("Index: {3}, Opcode: {0} {1},  Direction: {2}", Opcode, OpcodeName, Direction, Id));
            sb.AppendLine(Data.ToHexDump(OpcodeName, Opcode, 0, Data.Length, Direction));
            sb.AppendLine("**********************************************************");

            return sb.ToString();
        }

    }
}
